﻿using System.Collections.Generic;
using com.winter.bird;
using com.winter.pig;
using com.winter.view._02_game;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace com.winter.manager
{
    public class GameManager : MonoBehaviour
    {
        public enum game_STATE
        {
            ready,
            running,
            loss,
            win,
            pause
        }


        public game_STATE gameState;

        private static GameManager instance;

        public static GameManager getInstance()
        {
            return instance;
        }

        [Header("场景中的小鸟集合")] public List<Bird> birds;
        [Header("场景中的小猪集合")] public List<Pig> pigs;

        [Header("小鸟在弹弓上的初始位置")] [SerializeField]
        private Vector3 originPos;

        [Header("失败显示的uiPanel")] public GameObject lossPanel;
        [Header("胜利显示的uiPanel")] public GameObject winPanel;
        [Header("暂停显示的uiPanel")] public GameObject pausePanel;

        public GameManager()
        {
            instance = this;
        }

        private void Start()
        {
            // init variable
            originPos = birds[0].transform.position;

            initView();

            // init state
            gameState = game_STATE.running;
        }


        private void initView()
        {
            // 设置小鸟的位置
            for (int i = 0; i < birds.Count; i++)
            {
                if (i == 0)
                {
                    birds[i].transform.position = originPos;
                    birds[i].enabled = true;
                    birds[i].springJoint2D.enabled = true;
                }
                else
                {
                    birds[i].enabled = false;
                    birds[i].springJoint2D.enabled = false;
                }
            }
        }

        public void checkGameState()
        {
            if (pigs.Count > 0)
            {
                if (birds.Count > 0)
                {
                    // you still have birds, you can try again
                    initView();
                }
                else
                {
                    // change UI state
                    winPanel.SetActive(false);
                    pausePanel.SetActive(false);
                    lossPanel.SetActive(true);

                    // you don't have bird.
                    // change game state (loss)
                    gameState = game_STATE.loss;
                }
            }
            else
            {
                // change UI state
                lossPanel.SetActive(false);
                pausePanel.SetActive(false);
                winPanel.GetComponent<WinPanel>().win();

                // change game state (win)
                gameState = game_STATE.win;
            }
        }


        /// <summary>
        /// 重新玩
        /// 需要处理game manager的状态
        /// </summary>
        public void Replay()
        {
            // change scene
            SceneManager.LoadScene(sceneBuildIndex: 2);

            // change game state
            if (gameState != game_STATE.running)
            {
                Time.timeScale = 1;
                gameState = game_STATE.running;
            }
        }

        /// <summary>
        /// 跳转的菜单场景
        /// </summary>
        public void Home()
        {
            // change scene
            SceneManager.LoadScene(sceneBuildIndex: 1);

            // change game state
            gameState = game_STATE.ready;
        }
    }
}